package com.shaoshaohuo.app.ui;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.net.ShopHttpConfig;
import com.shaoshaohuo.app.ui.fragment.MyFarmOrderFragment;
import com.shaoshaohuo.app.ui.fragment.MyFarmShopFragment;
import com.shaoshaohuo.app.ui.fragment.ReleaseShopFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 发布商品页面
 * Created by Administrator on 2017/6/27.
 */

public class CollectivefarmActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private LinearLayout ac_clootive_topc;
    private ImageView ac_colltive_sanjiao;
    private ViewPager ac_colltive_vp;
    private LinearLayout ac_clootive_tvlinlist;
    private List<Fragment> list = new ArrayList<>();
    private MyFarmShopFragment myFarmShopFragment;
    private ReleaseShopFragment releaseShopFragment;
    private MyFarmOrderFragment myFarmOrderFragment;

    private ImageView kefu;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_colltive);
        changeTransltestaus(getWindow().getDecorView().findViewById(android.R.id.content), this, TransltestausModel.TOPPicter, -1);

        ViewGroup viewById = (ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content);
        View childAt = ((ViewGroup) ((ViewGroup) viewById.getChildAt(0)).getChildAt(0)).getChildAt(0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int statusBarHeight = getStatusBarHeight(this);
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) childAt.getLayoutParams();
            layoutParams.setMargins(0, statusBarHeight, 0, 0);
        }
        ac_clootive_topc = (LinearLayout) findViewById(R.id.ac_clootive_topc);
        ac_clootive_topc.getLayoutParams().height = getResources().getDisplayMetrics().heightPixels / 4;
        ac_colltive_sanjiao = (ImageView) findViewById(R.id.ac_colltive_sanjiao);
        ac_colltive_sanjiao.getLayoutParams().width = getResources().getDisplayMetrics().widthPixels / 3;
        ac_colltive_vp = (ViewPager) findViewById(R.id.ac_colltive_vp);
        ac_colltive_vp.addOnPageChangeListener(this);
        //list.add
        myFarmShopFragment = new MyFarmShopFragment();
        releaseShopFragment = new ReleaseShopFragment();
        myFarmOrderFragment = new MyFarmOrderFragment();
        list.add(releaseShopFragment);
        list.add(myFarmShopFragment);
        list.add(myFarmOrderFragment);
        ac_colltive_vp.setAdapter(new Mydapter(getSupportFragmentManager()));
        ac_clootive_tvlinlist = (LinearLayout) findViewById(R.id.ac_clootive_tvlinlist);

        for (int i = 0; i < ac_clootive_tvlinlist.getChildCount(); i++) {
            View childAt1 = ac_clootive_tvlinlist.getChildAt(i);
            childAt1.setOnClickListener(this);
        }
        findViewById(R.id.ac_colltive_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        kefu = (ImageView) findViewById(R.id.kefu);
        kefu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(ShopHttpConfig.phone());
            }
        });
    }

    private float start = 0;
    private float old = 0;

    @Override
    public void onPageScrolled(int position, float positionOffset, int i1) {
        old = positionOffset;
        if (old > start) {
            Log.e("TAG", "onPageScrolled 左滑: ");
            if (positionOffset != 0) {
                ac_colltive_sanjiao.setTranslationX(position * ac_colltive_sanjiao.getWidth() + positionOffset * ac_colltive_sanjiao.getWidth());
            }

        } else {
            Log.e("TAG", "onPageScrolled 右滑: ");
            if (positionOffset != 0) {
                ac_colltive_sanjiao.setTranslationX(((position + 1) * ac_colltive_sanjiao.getWidth()) - (1 - positionOffset) * ac_colltive_sanjiao.getWidth());
            }


        }
        start = old;
    }

    @Override
    public void onPageSelected(int i) {

    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    @Override
    public void onClick(View v) {
        int i = ac_clootive_tvlinlist.indexOfChild(v);
        if (ac_colltive_vp.getCurrentItem() != i) {
            ac_colltive_vp.setCurrentItem(i);
        }

    }


    private ShopCallBack shopCallBack;

    public void setShopCallBack(ShopCallBack shopCallBack) {
        this.shopCallBack = shopCallBack;
    }

    public interface ShopCallBack {
        void shopCallBack();
    }

    class Mydapter extends FragmentPagerAdapter {

        public Mydapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return list.get(i);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}
